import PDFMerger from 'pdf-merger-js';
import fs from "node:fs";

export async function pdfmerger(dir){
	let merger = new PDFMerger();
	let fls = fs.readdirSync(dir);
	//console.log(fls);return;
	for(let ff of fls){
		console.log(ff);
		if(ff.substr(-4).toLowerCase() == '.pdf'){
			console.log(dir + '/' + ff);
			await merger.add(dir + '/' + ff);
		}else{
			let stat = fs.statSync(dir + '/' + ff);
			if(stat.isDirectory()){
				let flssub = fs.readdirSync(dir + '/' + ff);
				for(let ffsub of flssub){
					if(ffsub.substr(-4).toLowerCase() == '.pdf'){
						console.log(dir + '/' + ff + "/" + ffsub);
						await merger.add(dir + '/' + ff + "/" + ffsub);
					}
				}
			}
		}
	}
	await merger.save('merged.pdf');
	console.log("合并完成")
}